#!/usr/bin env python
# -*- coding: utf-8 -*-
###########################################################################
#   Copyright (C) 2008 by Serj Poltavsky                                  #
#   serge.uliss at gmail.com                                              #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 3 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program. If not, see <http://www.gnu.org/licenses/>   #
###########################################################################

import os
import sys

class ConvertError(RuntimeError):
    pass

class Convert(object):
    def __init__(self, src, dest):
        self.src = src
        self.dest = dest
        self.progress_hook = self.console_progress

    def check_dest(self):
	if not self.is_dest_exists(self):
	    raise ConvertError('Destination file "%s" not exists' % self.src)

    def check_src(self):
	if not self.is_src_exists():
	    raise ConvertError('Source file "%s" not exists' % self.src)

    def convert_wait(self, run, msg):
        ret = run.wait()
        if ret != 0:
            print >>sys.stderr, msg

    def console_progress(self, percent):
        width = 40
        fmt = "%%3i %%%% [%%-%is] " % width
        progress = fmt % (percent, '=' * int(percent / (100.0/ width)))
        sys.stdout.write('\b' * len(progress))
        sys.stdout.write(progress)
        sys.stdout.flush()

    ''' Have to be implemented in derived class '''
    def convert(self):
        pass

    ''' Checks, if convertion need '''
    def is_converted(self):
        return self.is_dest_exists() and not self.is_dest_older()

    def is_dest_exists(self):
        return os.path.exists(self.dest)

    def is_dest_older(self):
        return os.stat(self.dest).st_mtime < os.stat(self.src).st_mtime

    def is_src_exists(self):
        return os.path.exists(self.src)

    def get_dest(self):
        return self.__dest

    def get_src(self):
        return self.__src

    ''' Runs convertion if it's needed '''
    def run(self):
        if not self.is_converted():
            self.convert()

    def set_dest(self, dest):
        self.__dest = dest

    def set_src(self, src):
        self.__src = src

    dest = property(get_dest, set_dest)
    src = property(get_src, set_src)